import pkg from '../../package.json';

import schemasBuilder from './graphql';
import modelsBuilder from './modelsBuilder';
import routes from './routes';
import taskQueueInit from './tasks';
import methodsBuilder from './methods';
import { plugin as billing } from './plugins/billing';
import systemRepeatTask from './systemRepeatTask';
import debugCheck from '@kuiper-helpers/debug-check/dist';
const { name, version } = pkg;

export const plugin = {
	name,
	version,
	dependencies: [
		'@kuiper/redis',
		'@kuiper/mongo',
		'@kuiper/graphql',
		'@kuiper/react-base',
		'@kuiper/auth',
	],
	async register(server, options) {
		server.methods.registerModulePermissions({
			module: 'cloud-monitor',
			permissions: {
				// #region job
				createJob: {
					id: 'createJob',
					name: '创建监测任务配置',
					defaultGrant: 1,
				},
				listJob: {
					id: 'listJob',
					name: '查看监测任务配置',
					defaultGrant: 1,
				},
				editJob: {
					id: 'editJob',
					name: '编辑监测任务配置',
					defaultGrant: 1,
				},
				removeJob: {
					id: 'removeJob',
					name: '删除监测任务配置',
					defaultGrant: 1,
				},
				// #endregion job

				// #region task
				listTask: {
					id: 'listTask',
					name: '查看监测任务',
					defaultGrant: 1,
				},
				editTask: {
					id: 'editTask',
					name: '编辑监测任务',
					defaultGrant: 1,
				},
				removeTask: {
					id: 'removeTask',
					name: '删除监测任务',
					defaultGrant: 1,
				},
				// #endregion task

				// #region alert-receiver
				createAlertReceiver: {
					id: 'createAlertReceiver',
					name: '创建告警人',
					defaultGrant: 1,
				},
				alertReceiverWithoutVerify: {
					id: 'alertReceiverWithoutVerify',
					name: '创建告警人无需验证',
				},
				listAlertReceiver: {
					id: 'listAlertReceiver',
					name: '查看告警人',
					defaultGrant: 1,
				},
				editAlertReceiver: {
					id: 'editAlertReceiver',
					name: '编辑告警人',
					defaultGrant: 1,
				},
				removeAlertReceiver: {
					id: 'removeAlertReceiver',
					name: '删除告警人',
					defaultGrant: 1,
				},
				// #endregion alert-receiver

				// #region alert-group
				createAlertGroup: {
					id: 'createAlertGroup',
					name: '创建告警组',
					defaultGrant: 1,
				},
				listAlertGroup: {
					id: 'listAlertGroup',
					name: '查看告警组',
					defaultGrant: 1,
				},
				editAlertGroup: {
					id: 'editAlertGroup',
					name: '编辑告警组',
					defaultGrant: 1,
				},
				removeAlertGroup: {
					id: 'removeAlertGroup',
					name: '删除告警组',
					defaultGrant: 1,
				},
				// #endregion alert-group

				// #region alert
				listAlert: {
					id: 'listAlert',
					name: '查看告警记录',
					defaultGrant: 1,
				},
				// #endregion alert

				// #region notificationSendLog
				listNotificationLog: {
					id: 'listNotificationSendLog',
					name: '查看告警推送记录',
					defaultGrant: 1,
				},
				// #endregion notificationSendLog

				// #region asset
				listAsset: {
					id: 'listAsset',
					name: '查看资产变动监测',
					defaultGrant: 1,
				},
				editAsset: {
					id: 'editAsset',
					name: '编辑资产变动监测',
					defaultGrant: 1,
				},
				// #endregion asset

				// #region whitelist
				listWhitelist: {
					id: 'listAsset',
					name: '查看白名单',
					defaultGrant: 1,
				},
				editWhitelist: {
					id: 'editAsset',
					name: '编辑白名单',
					defaultGrant: 1,
				},
				// #endregion whitelist
			},
		});

		methodsBuilder(server);
		modelsBuilder(server);
		schemasBuilder(server);
		server.route(routes);
		server.app.queues = taskQueueInit(server, options);

		try {
			await server.register(
				{ plugin: billing, options: options.billing },
				{ routes: { prefix: '/api/v1/billing' } }
			);
		} catch (error) {
			server.log.error({ error }, 'billing');
		}

		if (debugCheck('systemRepeatTask')) {
			systemRepeatTask(server);
		}
	},
};

export default { plugin };
